C/C++: Disable constant folding of address-vs-null comparisons#21258
Open
C/C++: Disable constant folding of address-vs-null comparisons#21258
Conversation
15ff5d6 to
a7c6736
Compare
a7c6736 to
8cfa0a3
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses an issue where the EDG frontend constant-folds address-vs-null comparisons (e.g., &var == NULL), causing the intermediate representation (IR) to lose VariableAddressInstruction nodes. The fix adds a containsAddressOf predicate to prevent constant folding of expressions containing address-of operators, ensuring that address operations remain visible in the IR for proper analysis.
Changes:
- Added
containsAddressOfpredicate to detect expressions containing address-of operations - Modified
isIRConstantto exclude expressions containing address-of operators - Added comprehensive test coverage for address-vs-null comparisons with global, local, and parameter variables
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll | Added containsAddressOf predicate and integrated it into isIRConstant to prevent constant folding of expressions containing address-of operators |
| cpp/ql/test/library-tests/ir/address_constant_folding/test.c | Comprehensive test file with various address-vs-null comparison scenarios covering global, local, and parameter variables |
| cpp/ql/test/library-tests/ir/address_constant_folding/variable_addresses.ql | Test query to verify VariableAddressInstruction nodes are present in the IR |
| cpp/ql/test/library-tests/ir/address_constant_folding/variable_addresses.expected | Auto-generated test expectations file (empty, will be populated when tests run) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The EDG frontend constant-folds address-vs-null comparisons (e.g.,
&var == NULL), causing the IR to lose theVariableAddressInstruction.This PR adds a
containsAddressOfpredicate used inisIRConstantso these expressions are expanded instead of collapsed.